package com.zmn.oms.services.impl.walldiscount;

import com.zmn.base.plat.engine.common.constant.PlatEngineConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.model.entity.walldiscount.WallDiscount;
import com.zmn.oms.model.entity.walldiscount.WallDiscountQuery;
import com.zmn.oms.persistence.interfaces.walldiscount.WallDiscountDao;
import com.zmn.oms.services.interfaces.walldiscount.WallDiscountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 墙面订单优惠信息接口实现类
 *
 * @author chengguang
 * @since  2022/06/20 18:46
 */
@Slf4j
@Service
public class WallDiscountServiceImpl implements WallDiscountService {

    private static final String TAG = "墙面订单优惠信息基础服务接口";

    @Resource
    private WallDiscountDao wallDiscountDao;

    @Override
    public WallDiscount findById(Long relateId) {
        if (Objects.isNull(relateId)) {
            log.error("[{}],根据id查询数据relateId为null", TAG);
            return null;
        }
        return wallDiscountDao.findById(relateId);
    }

    @Override
    public Integer findMaxSort() {
        return wallDiscountDao.findMaxSort();
    }

    @Override
    public List<WallDiscount> listByIds(List<Integer> relateIds) {
        if (CollectionUtils.isEmpty(relateIds)) {
            log.error("[{}],按条件查询数据,relateIds为null", TAG);
            return Collections.emptyList();
        }
        return wallDiscountDao.listByIds(relateIds);
    }

    @Override
    public List<WallDiscount> listByQuery(WallDiscountQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return Collections.emptyList();
        }
        return wallDiscountDao.listByQuery(query);
    }

    @Override
    public List<WallDiscount> listPageByQuery(WallDiscountQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return Collections.emptyList();
        }
        Integer count = countByQuery(query);
        query.setTotalCount(count);
        if (Objects.equals(count,PlatEngineConsts.NONE)){
            return new ArrayList<>();
        }
        return wallDiscountDao.listPageByQuery(query);
    }

    @Override
    public Integer countByQuery(WallDiscountQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return wallDiscountDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(WallDiscount wallDiscount) {
        if (Objects.isNull(wallDiscount.getRelateId())){
            log.error("[{}],根据id修改数据,wallDiscount为null", TAG);
            return null;
        }
        return wallDiscountDao.updateByKey(wallDiscount);
    }

    @Override
    public void updateStatusBatch(List<Integer> ids, Integer status, String operator) {
        if (!Objects.equals(status, PlatEngineConsts.NO) && !Objects.equals(status, PlatEngineConsts.YES)) {
            log.error("[{}],status状态超出取值范围", TAG);
            return;
        }
        wallDiscountDao.updateStatusBatch(ids, status, operator, DateUtil.getNow());
    }

    @Override
    public Integer insert(WallDiscount wallDiscount){
        if (Objects.isNull(wallDiscount)){
            log.error("[{}],新增数据,wallDiscount为null", TAG);
            return null;
        }
        return wallDiscountDao.insert(wallDiscount);
    }

    @Override
    public void insertBatch(List<WallDiscount> list){
        if (CollectionUtils.isEmpty(list)) {
            log.error("[{}],数据为空", TAG);
            return;
        }
        wallDiscountDao.insertBatch(list);
    }

    @Override
    public Integer deleteByKey(Long relateId) {
        if (Objects.isNull(relateId)){
            log.error("[{}],根据id删除数据,relateId为null", TAG);
            return null;
        }
        return wallDiscountDao.deleteByKey(relateId);
    }

    @Override
    public void deleteByWorkId(Long workId) {
        wallDiscountDao.deleteByWorkId(workId);
    }
}